﻿using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GZip的简单使用
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fileStreamRead = new FileStream("D:/test/config/config.txt", FileMode.OpenOrCreate, FileAccess.Read))
            {
                using (FileStream fileStreamWrite = new FileStream("D:/test/config/config-GZip.txt", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (GZipStream gZipStream = new GZipStream(fileStreamWrite, CompressionMode.Compress))
                    {
                        byte[] bytes = new byte[1024];  //1KB
                        int length;

                        //通过FileStream读取文件数据
                        while ((length = fileStreamRead.Read(bytes, 0, bytes.Length)) > 0)
                        {
                            //将读取的数据通过GZipStream压缩并输入到指定文件
                            gZipStream.Write(bytes, 0, length);
                        }
                    }
                }
            }
            using (FileStream fileStreamWrite = new FileStream("D:/test/config/config-GZip.txt", FileMode.OpenOrCreate, FileAccess.Read))
            {
                using (FileStream fileStreamRead = new FileStream("D:/test/config/config.txt", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (GZipStream gZipStream = new GZipStream(fileStreamWrite, CompressionMode.Decompress))
                    {
                        byte[] bytes = new byte[1024];  //1KB
                        int length;

                        //通过FileStream读取文件数据并解压
                        while ((length = gZipStream.Read(bytes, 0, bytes.Length)) > 0)
                        {
                            //将读取的数据通过GZipStream压缩并输入到指定文件
                            fileStreamWrite.Write(bytes, 0, length);
                        }
                    }
                }
            }
        }
    }
}
